$(document).ready(function() {
	$("#submit_btn").bind("click", savePrivilege);
	
	initPrivilegeTree();
});
var treeNodes; 
function initPrivilegeTree() {
	var userId = $("#hidden_userId").val();
	$.ajax({  
        async : false,  
        cache:false,  
        type: 'POST', 
        data: {comingParams:JSON.stringify({"userId" :StrUtil.strnull(userId)})},
        dataType : "json",  
        url: "userAction!getPrivilege.action",//请求的action路径  
        error: function () {//请求失败处理函数  
            alert('请求失败');  
        },  
        success:function(data){ //请求成功后处理函数。    
        	data = eval("(" + data + ")");
            treeNodes = data.data;   //把后台封装好的简单Json格式赋给treeNodes  
        }  
    });
	var zTreeDemo = $.fn.zTree.init($("#cityTree"),setting, treeNodes);
}

var setting = {
	check: {
		enable: true,
		chkStyle: "checkbox",
		chkboxType: {"Y":"ps","N":"ps"}
	},
	data: {
		simpleData: {
			enable: true
		}
	}
};

function count() {
	var zTree = $.fn.zTree.getZTreeObj("cityTree"),
	checkCount = zTree.getCheckedNodes(true).length,
	nocheckCount = zTree.getCheckedNodes(false).length,
	changeCount = zTree.getChangeCheckedNodes().length;
	alert('选中：'+checkCount+';未选中：'+nocheckCount);
}

//保存角色信息
function savePrivilege() {
	var userId = $("#hidden_userId").val();
	var jsarray = new Array();
	var user = {
			userAuth:{}
	};
	user.userId=userId;
	var zTree = $.fn.zTree.getZTreeObj("cityTree");
	var pri = zTree.getCheckedNodes(true);
	
	if (pri.length >0) {
		var jsarray2 = new Array();
		for(i=0;i<pri.length;i++) {
			var privilege = {};
			privilege.privilegeId = pri[i].id;
			jsarray2.push(privilege);
		}
		user.userAuth = jsarray2;
	}
	jsarray.push(user);
	var inparam = {data:{}};
	inparam.data=jsarray;
	inparam.type="savePrivilege";
	$.post("userAction!saveUser.action",{
		comingParams : JSON.stringify(inparam)
	},function(data) {
		data = eval("(" + data + ")");
		if (data.result = "true") {
			$.sucmodal("提示","保存成功" , function(){
				location.href="userManager.jsp";
			});
		} else {
			alert("保存失败");
		}
	});
}